var express=require("express");
var app=express();
var bodyParser = require("body-parser");
var fs=require("fs");
var cors=require("cors");
app.use(cors())

app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))

app.post("/api/user/login",function(req,res){
    res.header("Access-Control-Allow-Origin","*");

    console.log(req.body);
    // console.log(req);

    if (req.body.admin&&req.body.pws) {
        fs.readFile("username.json","utf-8",function(err,data){
            let datas=JSON.parse(data)
            if (datas[req.body.admin]) {
                fs.readFile(datas[req.body.admin].file+"","utf-8",function(err,data){
                    let info=JSON.parse(data)
                    if (req.body.pws==info.psd) {
                        res.send(JSON.stringify({
                            code:200,
                            message:"登录成功",
                            userinfo:info
                        }))
                    }else{
                        res.send( JSON.stringify({
                            code:500,
                            message:"用户名或密码错误,登录失败"
                        }))
                    }
                 })
            }else{
                res.send( JSON.stringify({
                    code:500,
                    message:"用户名或密码错误,登录失败"
                }))
            }
           
         })
    }else{
        res.send( JSON.stringify({
            code:500,
            message:"用户名或密码错误,登陆失败"
        }))
    }
   
})
var server=app.listen(3000,"localhost",function(){
    let host=server.address().address
    let port=server.address().port
    console.log("后台地址:http://%s:%s",host,port)
})
